package cn.anc.aonicardv.util;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.LocaleList;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import androidx.annotation.RequiresApi;

public class SupportLanguageUtil {
    private static Map<String, Locale> mSupportLanguages = new HashMap<String, Locale>(10) {{
        put(LanguageConstants.ENGLISH, new Locale("en", "US"));
        put(LanguageConstants.SIMPLIFIED_CHINESE, new Locale("zh", "CN"));
        put(LanguageConstants.TRADITIONAL_CHINESE, Locale.TRADITIONAL_CHINESE);
        put(LanguageConstants.LITHUANIAN, new Locale("lt", "LT"));
        put(LanguageConstants.ESTONIAN, new Locale("et", "EE"));
        put(LanguageConstants.LATVIAN, new Locale("lv", "LV"));
        put(LanguageConstants.POLISH, new Locale("pl", "PL"));
        put(LanguageConstants.ROMANIA, new Locale("ro", "RO"));
        put(LanguageConstants.RUSSIAN, new Locale("ru", "RU"));
        put(LanguageConstants.UKRAINE, new Locale("ru", "RU"));
        put(LanguageConstants.SLOVAK, new Locale("sk", "SK"));
        put(LanguageConstants.SPANISH, new Locale("es", "ES"));
        put(LanguageConstants.GERMAN, new Locale("de", "DE"));
        put(LanguageConstants.ITALIAN, new Locale("it", "IT"));
        put(LanguageConstants.FRENCH, new Locale("fr", "FR"));
        put(LanguageConstants.CZECH, new Locale("cs", "CS"));
        put(LanguageConstants.HUNGARY, new Locale("hu", "HU"));

    }};

    /**
     * 是否支持此语言
     *
     * @param language language
     * @return true:支持 false:不支持
     */
    public static boolean isSupportLanguage(String language) {
        return mSupportLanguages.containsKey(language);
    }

    /**
     * 获取支持语言
     *
     * @param language language
     * @return 支持返回支持语言，不支持返回系统首选语言
     */
    @TargetApi(Build.VERSION_CODES.N)
    public static Locale getSupportLanguage(String language) {
        if (isSupportLanguage(language)) {
            return mSupportLanguages.get(language);
        }
        return getSystemPreferredLanguage();
    }

    /**
     * 获取系统首选语言
     *
     * @return Locale
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Locale getSystemPreferredLanguage() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }
}
